import * as Types from '../action-types';
import {getSliders,getLessons} from '../../api/home'
export const setCurrentLesson=(val)=>{
    return {
        type:Types.SET_CURRENT_LESSON,
        val
       }
}
//如果使用reduxThunk actionCreator 可以返回一个函数 函数中有dispatch参数
export const getSlider=()=>(dispatch,getState)=>{
    getSliders().then(sliders=>{
        dispatch({
            type:Types.GET_SLIDERS,
            sliders
        })
    })
}

export const getLesson=()=>(dispatch,getState)=>{
    //从redux取出type,offset,limit   getState=store.getState
    let {currentLesson,lesson:{hasMore,limit,offset}}=getState().home //二次解构
    if(!hasMore)return //没有就不要获取
    //将isLoading改成true
    dispatch({type:Types.SET_LOADING_STATUS})
    getLessons(currentLesson,offset,limit).then(lesson=>{
        dispatch({
            type:Types.GET_LESSONS,
            ...lesson
            // lessons:lesson.lessons,
            // hasMore:lesson.hasMore,
        })
    })
}